﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using DbSharp.Tools.DbConnectionBuilder.Core.Plugin;


namespace DbSharp.Tools.DbConnectionBuilder.Core
{


    class PluginProvider
    {
        //TODO: ConnectionBuilder 1: Add new Plugin object
        private IDbConnectionPlugin m_OracleConnectionPlugin2 = new OracleConnectionPlugin2();
        private IDbConnectionPlugin m_OracleConnectionPlugin = new OracleConnectionPlugin();
        private IDbConnectionPlugin m_MsSqlConnectionPlugin = new MsSqlConnectionPlugin();
        private IDbConnectionPlugin m_ExcelOleConnectionPlugin = new ExcelOleConnectionPlugin();
        private IDbConnectionPlugin m_Excel2007OleConnectionPlugin = new Excel2007OleConnectionPlugin();
        private IDbConnectionPlugin m_MySqlConnectionPlugin = new MySqlConnectionPlugin();
        private IDbConnectionPlugin m_PostgresqlConnectionPlugin = new PostgresqlConnectionPlugin();

        private List<PluginNamePair> m_ListPluginNamePair = new List<PluginNamePair>();

        public PluginProvider()
        {
            //TODO: ConnectionBuilder 2: Add new Plugin NamePair
            m_ListPluginNamePair.Add(m_OracleConnectionPlugin2.PluginNamePair);
            m_ListPluginNamePair.Add(m_OracleConnectionPlugin.PluginNamePair);
            m_ListPluginNamePair.Add(m_MsSqlConnectionPlugin.PluginNamePair);
            m_ListPluginNamePair.Add(m_ExcelOleConnectionPlugin.PluginNamePair);
            m_ListPluginNamePair.Add(m_Excel2007OleConnectionPlugin.PluginNamePair);
            m_ListPluginNamePair.Add(m_MySqlConnectionPlugin.PluginNamePair);
            m_ListPluginNamePair.Add(m_PostgresqlConnectionPlugin.PluginNamePair);
        }

        public IDbConnectionPlugin ChoosePlugin(string pluginDisplayName)
        {
            IDbConnectionPlugin connectionPlugin = null;
            string pluginName = pluginDisplayName.Trim();

            //TODO: ConnectionBuilder 3: Add new Plugin Driver name
            if (string.Equals(pluginName, m_OracleConnectionPlugin2.PluginNamePair.DisplayName, StringComparison.OrdinalIgnoreCase))
            {
                connectionPlugin = m_OracleConnectionPlugin2;
            }
            else if (string.Equals(pluginName, m_OracleConnectionPlugin.PluginNamePair.DisplayName, StringComparison.OrdinalIgnoreCase))
            {
                connectionPlugin = m_OracleConnectionPlugin;
            }
            else if (string.Equals(pluginName, m_MsSqlConnectionPlugin.PluginNamePair.DisplayName, StringComparison.OrdinalIgnoreCase))
            {
                connectionPlugin = m_MsSqlConnectionPlugin;
            }
            else if (string.Equals(pluginName, m_ExcelOleConnectionPlugin.PluginNamePair.DisplayName, StringComparison.OrdinalIgnoreCase))
            {
                connectionPlugin = m_ExcelOleConnectionPlugin;
            }
            else if (string.Equals(pluginName, m_Excel2007OleConnectionPlugin.PluginNamePair.DisplayName, StringComparison.OrdinalIgnoreCase))
            {
                connectionPlugin = m_Excel2007OleConnectionPlugin;
            }
            else if (string.Equals(pluginName, m_MySqlConnectionPlugin.PluginNamePair.DisplayName, StringComparison.OrdinalIgnoreCase))
            {
                connectionPlugin = m_MySqlConnectionPlugin;
            }
            else if (string.Equals(pluginName, m_PostgresqlConnectionPlugin.PluginNamePair.DisplayName, StringComparison.OrdinalIgnoreCase))
            {
                connectionPlugin = m_PostgresqlConnectionPlugin;
            }
            else
            {
                throw new NotImplementedException("This Db is not implemented yet.");
            }
            return connectionPlugin;
        }

        public IDbConnectionPlugin ChoosePlugin(string providerName, string connectionString)
        {
            IDbConnectionPlugin connectionPlugin = null;
            providerName = providerName.Trim();

            //TODO: ConnectionBuilder 4: Add new Plugin Provider name
            if (string.Equals(providerName, m_OracleConnectionPlugin2.PluginNamePair.ProviderName, StringComparison.OrdinalIgnoreCase))
            {
                connectionPlugin = m_OracleConnectionPlugin2;
            }
            else if (string.Equals(providerName, m_OracleConnectionPlugin.PluginNamePair.ProviderName, StringComparison.OrdinalIgnoreCase))
            {
                connectionPlugin = m_OracleConnectionPlugin;
            }
            else if (string.Equals(providerName, m_MsSqlConnectionPlugin.PluginNamePair.ProviderName, StringComparison.OrdinalIgnoreCase))
            {
                connectionPlugin = m_MsSqlConnectionPlugin;
            }
            else if (string.Equals(providerName, m_ExcelOleConnectionPlugin.PluginNamePair.ProviderName, StringComparison.OrdinalIgnoreCase))
            {
                var builder = new OleDbConnectionStringBuilder(connectionString);
                object extendedProperties;
                int index;
                if (builder.TryGetValue("Extended Properties", out extendedProperties))
                {
                    string xproperty = extendedProperties.ToString().Trim().ToUpper();
                    index = xproperty.IndexOf(@"Excel", StringComparison.OrdinalIgnoreCase);
                    if (index > -1)
                    {
                        connectionPlugin = m_ExcelOleConnectionPlugin;
                    }
                }
            }
            else if (string.Equals(providerName, m_Excel2007OleConnectionPlugin.PluginNamePair.ProviderName, StringComparison.OrdinalIgnoreCase))
            {
                var builder = new OleDbConnectionStringBuilder(connectionString);
                object extendedProperties;
                int index;
                if (builder.TryGetValue("Extended Properties", out extendedProperties))
                {
                    string xproperty = extendedProperties.ToString().Trim().ToUpper();
                    index = xproperty.IndexOf(@"Excel", StringComparison.OrdinalIgnoreCase);
                    if (index > -1)
                    {
                        connectionPlugin = m_Excel2007OleConnectionPlugin;
                    }
                }
            }
            else if (string.Equals(providerName, m_MySqlConnectionPlugin.PluginNamePair.ProviderName, StringComparison.OrdinalIgnoreCase))
            {
                connectionPlugin = m_MySqlConnectionPlugin;
            }
            else if (string.Equals(providerName, m_PostgresqlConnectionPlugin.PluginNamePair.ProviderName, StringComparison.OrdinalIgnoreCase))
            {
                connectionPlugin = m_PostgresqlConnectionPlugin;
            }
            else
            {
                throw new NotImplementedException("This Db is not implemented yet.");
            }
            return connectionPlugin;
        }

        public List<PluginNamePair> GetSupportedDbPlugins()
        {
            return m_ListPluginNamePair;
        }


    }
}
